//
//  TableViewController.swift
//  mm
//
//  Created by Qingke on 15/4/6.
//  Copyright © 2015年 Qingke. All rights reserved.
//

import UIKit

class TableViewController: UITableViewController {
    var mm = [Beauty]()

    override func viewDidLoad() {
        super.viewDidLoad()
//        loadSampleData()
        if let save = loadMM() {
            mm += save
            
        }

        
        navigationItem.leftBarButtonItem = editButtonItem()
       // navigationItem.leftBarButtonItem?.title = "编辑"

    }
    /*
    //edit变成中文
    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        if editing {
            navigationItem.leftBarButtonItem?.title = "完成"
        } else {
            navigationItem.leftBarButtonItem?.title = "编辑"
        }
    }
    */
    func loadSampleData() {
        let mma = Beauty(name: "碧瑶", photo: UIImage(named: "mma"), ranting: 4)!
        let mmb = Beauty(name: "陆雪琪", photo: UIImage(named: "mmb"), ranting: 5)!
        let mmc = Beauty(name: "杨幂", photo: UIImage(named: "mmc"), ranting: 3)!
        mm = [mma , mmb ,mmc]
    }
    
    @IBAction func unwindToBeautyList(segue:UIStoryboardSegue) {
        if let sourceViewController = segue.sourceViewController as? ViewController,beauty = sourceViewController.beauty {
            if let selectedpath = tableView.indexPathForSelectedRow {
                mm[selectedpath.row] = beauty
                tableView.reloadRowsAtIndexPaths([selectedpath], withRowAnimation: .Right)
            } else{
            let newIndexpath = NSIndexPath(forRow: mm.count, inSection: 0)
            mm.append(beauty)
            tableView.insertRowsAtIndexPaths([newIndexpath], withRowAnimation: .Bottom)
            }
            saveMM()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return mm.count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("beautycell", forIndexPath: indexPath) as! TableViewCell
        let m = mm[indexPath.row]
        cell.nameLabel.text = m.name
        cell.BeautyImage.image = m.photo
        cell.rant.ranting = m.ranting

        // Configure the cell...

        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 99
    }

    /*
    // 可不可以编辑的方法
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            mm.removeAtIndex(indexPath.row)
            saveMM()
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "edit" {
            
            //find girl
            let selectedCell = sender as! TableViewCell
            let indexPath = tableView.indexPathForCell(selectedCell)
            let selectedBeauty = mm[indexPath!.row]
            
            //把美女传给目标ViewController
            let beautyViewController = segue.destinationViewController as! ViewController
            beautyViewController.beauty = selectedBeauty
        } else {
            
        }
        
    }
    func saveMM() {
        let isSaveSuccess = NSKeyedArchiver.archiveRootObject(mm, toFile: Beauty.ArchiveURL.path!)
        if !isSaveSuccess {
            print("保存失败")
        }
    }
    func loadMM() -> [Beauty]? {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(Beauty.ArchiveURL.path!) as? [Beauty]
    }
    

}
